package net.realsee.service.wcs.work.impl;

import cn.hutool.core.bean.BeanUtil;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.rcs.hik.HikWarnData;
import net.realsee.domain.wcs.work.WcsWarn;
import net.realsee.domain.wcs.work.bo.WcsWarnBo;
import net.realsee.domain.wcs.work.vo.WcsWarnVo;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.mapper.wcs.work.WcsWarnMapper;
import net.realsee.service.wcs.work.IWcsWarnService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 系统告警信息Service业务层处理
 *
 * @author realsee
 * @date 2023-08-24
 */
@RequiredArgsConstructor
@Service
public class WcsWarnServiceImpl extends ServiceImpl<WcsWarnMapper, WcsWarn> implements IWcsWarnService {

    private final WcsWarnMapper baseMapper;

    /**
     * 查询系统告警信息
     */
    @Override
    public WcsWarnVo selectVoById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询系统告警信息列表
     */
    @Override
    public TableDataInfo<WcsWarnVo> selectVoPage(WcsWarnBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsWarn> lqw = buildVoQueryWrapper(bo);
        Page<WcsWarnVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询系统告警信息列表
     */
    @Override
    public List<WcsWarnVo> selectVoList(WcsWarnBo bo) {
        LambdaQueryWrapper<WcsWarn> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsWarn> buildVoQueryWrapper(WcsWarnBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsWarn> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWarnClass()), WcsWarn::getWarnClass, bo.getWarnClass());
        lqw.eq(StringUtils.isNotBlank(bo.getWarnType()), WcsWarn::getWarnType, bo.getWarnType());
        lqw.eq(StringUtils.isNotBlank(bo.getWarnStatus()), WcsWarn::getWarnStatus, bo.getWarnStatus());
        lqw.eq(bo.getStartedOn() != null, WcsWarn::getStartedOn, bo.getStartedOn());
        lqw.eq(bo.getPriority() != null, WcsWarn::getPriority, bo.getPriority());
        lqw.orderByDesc(WcsWarn::getStartedOn);
        return lqw;
    }

    /**
     * 新增系统告警信息
     */
    @Override
    public Boolean insertByBo(WcsWarnBo bo) {

        List<WcsWarn> wcsWarnList = baseMapper.selectListByWarnClassAndWarnSubject(bo);

        if (!wcsWarnList.isEmpty()) {
            // 先逻辑删除
            String warnClass = bo.getWarnClass();
            String warnSubject = bo.getWarnSubject();
            baseMapper.updateByWarnClassAndWarnSubject(warnClass, warnSubject);

            // 后插入数据
            return saveWarn(bo);
        }
        return saveWarn(bo);
        //  WcsWarn add = BeanUtil.toBean(bo, WcsWarn.class);
        //  return this.save(add);
    }

    /**
     * 插入告警信息
     * @param bo
     * @return
     */
    private Boolean saveWarn(WcsWarnBo bo) {
        WcsWarn add = BeanUtil.toBean(bo, WcsWarn.class);
        add.setStartedOn(new Date());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改系统告警信息
     */
    @Override
    public Boolean updateByBo(WcsWarnBo bo) {
        WcsWarn update = BeanUtil.toBean(bo, WcsWarn.class);
        return this.updateById(update);
    }

    /**
     * 批量删除系统告警信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }

    /**
     * 告警推送
     */
    @Override
    public void insertWarnData(List<HikWarnData> dataList) {
        // 1.先删除
        baseMapper.deleteWarnData();
        // 2.批量插入
        for (HikWarnData data : dataList) {
            WcsWarn wcsWarn = new WcsWarn();
            wcsWarn.setStartedOn(data.getBeginDate());
            wcsWarn.setMessage(data.getWarnContent());
            wcsWarn.setOrderNo(data.getTaskCode());
            wcsWarn.setWarnSubject(data.getRobotCode());
            baseMapper.insert(wcsWarn);
        }
    }
}
